//: polymorphism/shape/RandomShapeGenerator.java
// A "factory" that randomly creates shapes.
package polymorphism.shape;

import java.util.*;

public class RandomShapeGenerator implements Iterable<Shape> {
	private Random rand = new Random(47);
	private int max;

	public Shape next() {
		switch (rand.nextInt(3)) {
		default:
		case 0:
			return new Circle();
		case 1:
			return new Square();
		case 2:
			return new Triangle();
		}
	}
	
	public Shape generate() {
		switch (rand.nextInt(3)) {
		default:
		case 0:
			return new Circle();
		case 1:
			return new Square();
		case 2:
			return new Triangle();
		}
	}

	public RandomShapeGenerator(int max) {
		this.max = max;
	}

	@Override
	public Iterator<Shape> iterator() {
		return new Iterator<Shape>() {
			private int current = 0;
			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}

			@Override
			public Shape next() {
				if (current < max){
					current++;
					return generate();
				}else{
					return null;
				}
			}

			@Override
			public boolean hasNext() {
				return current < max;
			}
		};
	}
} // /:~
